/**
 * 产品信息product.js
 * 版本号： v1.0.0
 */
$(function () {
    //绑定供应商列表
    bindCombo_supplierList("supId")

    //设置输入框的最大输入长度
    $(".easyui-textbox").each(function () {
        var data_options = $(this).attr("data-options");
        if (data_options) {
            var matchs = data_options.match(/length\[1,(\d*)\]/);
            if (matchs) {
                $(this).textbox("textbox").attr("maxlength", matchs[1] - 0 + 1);
            } else {
                $(this).textbox("textbox").attr("maxlength", 20);
            }
        } else {
            $(this).textbox("textbox").attr("maxlength", 20);
        }
    });


    /**
     * 产品列表
     */
    var lastChecked = 0;
    $("#product_dataList").datagrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        striped: true,
        pageSize: 10,
        pageNumber: 1,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/product/productList.do',
        idField: 'proId',
        frozenColumns: [[
            {field: 'proId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'materialName', title: '产品名称', width: 200, fixed: true},
                {field: 'standardType', title: '规格型号', width: 150, fixed: true},
                {field: 'unitPrice', title: '产品单价', width: 80, fixed: true,
                    formatter:function (value) {
                        if (value)
                            return value.toFixed(2);
                    }
                },
                {
                    field: 'supplier', title: '供应商名称', width: 220,fixed: true,
                    formatter: function (value) {
                        if (value) {
                            return value.supName
                        }
                    }
                },
                {
                    field: 'mobile_', title: '供应商电话', width: 120,
                    formatter: function (value,row) {
                        if (row.supplier) {
                            return row.supplier.mobile
                        }

                    }
                },
            ]
        ],
        onCheck: function (rowIndex, rowData) {
            var row = $("#product_dataList").datagrid("getChecked");
            if (row && rowIndex == lastChecked) {
                $('#product_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            } else {
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#product_dataList").datagrid("selectRow", 0);
            }
        }
    });


    //定义可操作按钮
    $("#product_menu").dialog({
        title: '',
        width: 'auto',
        height: 35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });


});


/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for (var j = 0; j < btnOptions.length; j++) {
        if (false != btnOptions[j].value) {
            falg = true;
            break;
        }
    }
    if (!falg) {
        //不存在按钮
        toolbar.push({});
    } else {
        //存在按钮
        for (var i = 0; i < btnOptions.length; i++) {
            if (false != btnOptions[i].value) {
                if ("searchBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id: btnOptions[i].value.meId,
                        text: btnOptions[i].value.name,
                        iconCls: btnOptions[i].value.iconCls,
                        handler: function () {
                            product_loadSearch();
                        }
                    });
                }
                //添加按钮
                if ("addBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id: btnOptions[i].value.meId,
                        text: btnOptions[i].value.name,
                        iconCls: btnOptions[i].value.iconCls,
                        handler: function () {
                            window.location.href = CONTEXT_PATH + "/product/toProduct.do";
                        }
                    });
                }
                //修改按钮
                if ("editBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id: btnOptions[i].value.meId,
                        text: btnOptions[i].value.name,
                        iconCls: btnOptions[i].value.iconCls,
                        handler: function () {
                            product_edit();
                        }
                    });
                }
                //删除按钮
                if ("delBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id: btnOptions[i].value.meId,
                        text: btnOptions[i].value.name,
                        iconCls: btnOptions[i].value.iconCls,
                        handler: function () {
                            product_del();
                        }
                    });
                }
                //查看详情按钮
                if ("viewBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id: btnOptions[i].value.meId,
                        text: btnOptions[i].value.name,
                        iconCls: btnOptions[i].value.iconCls,
                        handler: function () {
                            product_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 查看产品
 */
function product_view(iconCls) {
    var row = $("#product_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({title: '操作提示', msg: "请选择需要查看的产品详情", timeout: 3000, showType: "slide"});
        return;
    }
    var row = $("#product_dataList").datagrid("getSelected");
    var proIds = row.proId;
    //IFRAM打开窗口
    layoutSub(cpxq.id, cpxq.url + row.proId, iconCls, cpxq.closable, cpxq.title);
}


/**
 * 删除产品信息
 */
function product_del() {
    var checked = $("#product_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({title: '操作提示', msg: "请选择要删除的产品信息", timeout: 3000, showType: "slide"});
        return;
    }
    ;
    var row = $("#product_dataList").datagrid("getSelected");
    $.messager.confirm('删除提示', '您确定要删除 ' + row.materialName + ' 吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/product/del.do?proId=" + row.proId,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#product_dataList").datagrid("reload", {
                        supId: $("#supId").textbox("getValue"),
                        materialName: $("#materialName").textbox("getValue")
                    });
                }
            })
        }
    });
}


/**
 * 修改产品信息
 * @returns {boolean}
 */
function product_edit() {
    var checked = $("#product_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({title: '操作提示', msg: "请选择要修改的产品信息", timeout: 3000, showType: "slide"});
        return;
    }
    ;
    var row = $("#product_dataList").datagrid("getSelected");
    window.location.href = CONTEXT_PATH + "/product/toProduct.do?proId=" + row.proId;
}


/**
 * 供应商bind事件
 * @param deviceControl
 */
function bindCombo_supplierList(supplierControl) {
    var companyId = $("#companyId_").val();
    $.ajax({
        url: CONTEXT_PATH + "/supplier/getSupplierList.do",
        data:{
            "companyId":companyId
        },
        type: "get",
        success: function (data) {
            var supplierCombo = [{'text': '--请选择--', 'value': ''}];
            for (var i = 0; i < data.length; i++) {
                supplierCombo.push({"text": data[i].supName, "value": data[i].supId});
            }
            $("#" + supplierControl).combobox("loadData", supplierCombo);
        }
    })
}

/**
 *  加载查询
 */
function product_loadSearch() {
    //刷新dg页码
    refreshPageNumber("product_dataList");
    //查询数据
    $("#product_dataList").datagrid("reload", {
        supId: $("#supId").textbox("getValue"),
        materialName: $("#materialName").textbox("getValue")
    });
}












